package com.amc.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.amc.constant.MerchantConstant;
import com.amc.entity.Merchant;
import com.amc.service.IMerchantService;
import com.amc.service.impl.MerchantServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Servlet implementation class MerchantApplyServlet
 */
public class MerchantApplyServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private IMerchantService service = new MerchantServiceImpl();
	private ObjectMapper mapper = new ObjectMapper();
	private SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public MerchantApplyServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
		HttpServletResponse response) throws ServletException, IOException {
		dosth(request, response);
	}
	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		dosth(request, response);
	}
	

	private void dosth(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String merchantId = request.getParameter("merchantId");
		String type = request.getParameter("type");
		//最后修改时间
		String lastApplyTime = request.getParameter("lastApplyTime");
		
		Merchant merchant = service.queryByMerchantId(merchantId);
		Map<String, Object> resultMap = new HashMap<>();
		PrintWriter writer = response.getWriter();
		if("apply".equals(type) || "reject".equals(type)){
			if(String.valueOf(merchant.getLastApplyTime()).equals(lastApplyTime)){
				if("apply".equals(type)){
					merchant.setAuditStatus(MerchantConstant.AUDIT_STATUS_PASS);
					//merchant.setRemarks("审核通过！");
				}
				if("reject".equals(type)){
					String remarks = request.getParameter("remarks");
					System.out.println(remarks);
					merchant.setAuditStatus(MerchantConstant.AUDIT_STATUS_FAIL);
					merchant.setRemarks(remarks);
				}
				boolean result = service.update(merchant);
				if(result == true) {
					resultMap.put("status", "success");
					resultMap.put("msg", "");
					
				} else {
					resultMap.put("status", "fail");
					resultMap.put("msg", "操作失败！");
				
				}

			}else{
				resultMap.put("status", "fail");
				resultMap.put("msg", "该用户已经跟新了申请信息！");
			}
		}else if("isBlack".equals(type)){
			int isBlack = Integer.parseInt(request.getParameter("isBlack"));
			merchant.setIsBlack(isBlack);
			boolean result = service.update(merchant);
			if(result == true) {
				resultMap.put("status", "success");
				resultMap.put("msg", "");
				
			} else {
				resultMap.put("status", "fail");
				resultMap.put("msg", "操作失败！");
			
			}

		}
		
		String json = mapper.writeValueAsString(resultMap);
		writer.write(json);
		writer.close();
	}
}
